﻿using System;
using System.Configuration;
using System.Globalization;
using System.Threading;
using System.Windows.Forms;
using E4RPTools;

namespace Fitecom
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Thread.CurrentThread.CurrentUICulture = new CultureInfo("pt-PT");
            var licenseKey = ConfigurationManager.AppSettings["Licenca"];
            //ExceptionPublisher.Publish(ConfigurationManager.AppSettings["FicheiroErros"].ToString(), new Exception("Valida Data"));
            if (DateTime.Now > new DateTime(2014, 1, 1))
                return;
            //if (!E4RPLicense.License.Validate(E4RPTools.Product.Fitecom.ToString(), (long)E4RPTools.Product.Fitecom, 1, licenseKey))
            //    return;
            //ExceptionPublisher.Publish(ConfigurationManager.AppSettings["FicheiroErros"].ToString(), new Exception("Valida Access"));
            var controller = new Controller { LogFile = ConfigurationManager.AppSettings["FicheiroErros"] };
            if (!controller.IsAccessConnectionValid())
                return;
            //ExceptionPublisher.Publish(ConfigurationManager.AppSettings["FicheiroErros"].ToString(), new Exception("Valida Database"));
            var user = ConfigurationManager.AppSettings["User"];
            var password = ConfigurationManager.AppSettings["Password"];            
            if (!controller.IsUserValid(user, password))
                return;
            //ExceptionPublisher.Publish(ConfigurationManager.AppSettings["FicheiroErros"].ToString(), new Exception("Valida Argumentos"));
            if (args.Length == 1)
                return;
            //ExceptionPublisher.Publish(ConfigurationManager.AppSettings["FicheiroErros"].ToString(), new Exception("Valida Primavera"));
            if (!controller.OpenPrimaveraCompany())
                return;
            switch (args.Length)
            {
                case 0:
                    var main = new MainRibbonForm { Controller = controller };
                    Application.Run(main);
                    break;
                case 2:
                    controller.Import(args[0], args[1]);
                    controller.CloseConnections();
                    break;
            }
            //controller.Import("ActualizarItemsStock", "18708AFE-15B7-11E3-BB96-3085A9020D6D");
            //controller.CloseConnections();
            
        }


    }
}
